package com.cz.video.website.controller;

import com.cz.video.common.annotation.LegalValidate;
import com.cz.video.common.annotation.Log;
import com.cz.video.common.core.controller.BaseController;
import com.cz.video.common.core.domain.PageQuery;
import com.cz.video.common.core.domain.R;
import com.cz.video.common.core.page.TableDataInfo;
import com.cz.video.common.enums.BusinessType;
import com.cz.video.common.helper.LoginHelper;
import com.cz.video.website.domain.bo.WebsiteCollectBo;
import com.cz.video.website.domain.vo.WebsiteCollectVo;
import com.cz.video.website.service.IWebsiteCollectService;
import com.cz.video.website.service.IWebsiteVideoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 * 收藏表 前端控制器
 * </p>
 *
 * @author yr
 * @since 2023-10-15
 */
@Tag(name="收藏表")
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/website/collect")
public class WebsiteCollectController extends BaseController {
    private final IWebsiteCollectService websiteCollectService;
    private final IWebsiteVideoService videoService;

    @Operation(summary = "添加收藏")
    @Log(title = "添加收藏", businessType = BusinessType.INSERT)
    @PostMapping("")
    @LegalValidate
    public R<Void> add(@RequestBody @Validated WebsiteCollectBo websiteCollect){
        websiteCollect.setUserId(LoginHelper.getUserId());
        return toAjax(websiteCollectService.insertCollect(websiteCollect));
    }

    @Operation(summary = "删除收藏")
    @Log(title = "删除收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> deleteById(@PathVariable Long[] id){
        return toAjax(websiteCollectService.deleteById(id));
    }

    @Operation(summary = "删除收藏(根据收藏夹和视频id)")
    @Log(title = "删除收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("")
    public R<Void> delete(Long collectFolderId,Long videoId){
        return toAjax(websiteCollectService.delete(collectFolderId,videoId));
    }

    /**
     * 修改收藏
     */
    @Operation(summary = "修改收藏")
    @Log(title = "收藏管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody WebsiteCollectBo websiteCollect) {
        websiteCollect.setUserId(LoginHelper.getUserId());
        return toAjax(websiteCollectService.updateCollect(websiteCollect));
    }

    /**
     * 查询收藏列表
     */
    @Operation(summary = "查询收藏")
    @GetMapping("/selectCollect")
    public TableDataInfo<WebsiteCollectVo> selectUser(WebsiteCollectBo websiteCollect, PageQuery pageQuery) {
        TableDataInfo<WebsiteCollectVo> page = websiteCollectService.selectPageCollectList(websiteCollect, pageQuery);
        videoService.setCover(Optional.of(page.getRows()).map(collectList->
            collectList.stream().map(WebsiteCollectVo::getVideoVo)
                .filter(Objects::nonNull).collect(Collectors.toList())
        ).orElse(null));
        return page;
    }
}
